package com.hyfrogx.modules.app.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.exception.ServiceException;
import com.hyfrogx.common.param.Constants;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.common.utils.VersionCompareUtils;
import com.hyfrogx.common.validator.ValidatorUtils;
import com.hyfrogx.modules.app.entity.HyClientVersion;
import com.hyfrogx.modules.app.form.AdApkFrom;
import com.hyfrogx.modules.app.form.ClientVersionForm;
import com.hyfrogx.modules.app.form.video.VodQueryForm;
import com.hyfrogx.modules.app.form.video.VodVideoVo;
import com.hyfrogx.modules.app.service.HyClientVersionService;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.charge.service.HyChargeService;
import com.hyfrogx.modules.manage.entity.HyAdEntity;
import com.hyfrogx.modules.manage.entity.HyProductionScaleEntity;
import com.hyfrogx.modules.manage.entity.smallVideo.HyVideoSmallEntity;
import com.hyfrogx.modules.manage.entity.video.HyVideoTypeEntity;
import com.hyfrogx.modules.manage.form.ad.AdVo;
import com.hyfrogx.modules.manage.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0.0
 * @Author: lfh
 * @Date: 2019/10/30 17:38
 * @Company 汉咏科技
 */
@RestController
@RequestMapping("/api/free")
@Api(value = "AskController", tags = {"客户端--免登录客户端接口"})
public class ApiFreeController {

  @Autowired
  private HyAdService hyAdService;

  @Autowired
  HyChargeService hyChargeService;

  @Autowired
  HyProductionVacuateService hyProductionVacuateService;

  @Autowired
  HyVideoVodService hyVideoVodService;

  @Autowired
  HyVideoTypeService hyVideoTypeService;

  @Autowired
  HyVideoSmallService hyVideoSmallService;

  @Autowired
  HyVideoSmallCommentService hyVideoSmallCommentService;

  @Autowired
  HyClientVersionService hyClientVersionService;

  /**
   * @param form
   * @return HyClientVersion 获取当前客户端的最新版本,是最新版返回版本对象
   * @throws Exception
   */
  @PostMapping("/jugeClientVersion")
  @ApiOperation("获取当前客户端的最新版本 🐉")
  public R jugeClientVersion(ClientVersionForm form) {
    try {
      HyClientVersion clientVersion = hyClientVersionService.getOne(new QueryWrapper<HyClientVersion>().eq(
              "type",form.getType()).eq("version_code",form.getVersionCode()));
      // 比较版本号
      int code = VersionCompareUtils.compareVersion(form.getVersionCode(),clientVersion.getVersionCode());
      if (clientVersion != null && code != -1) {
        return R.ok().put("data",clientVersion);
      } else {
        return R.error("当前还不是最新版本");
      }
    } catch (Exception e) {
      // TODO Auto-generated catch block
      return R.error(e.getMessage());
    }
  }


  @PostMapping("/ad/getList")
  @ApiOperation("手机获取广告列表")
  public R getAdList(@RequestBody @Validated AdApkFrom adApkFrom) {
    List<AdVo> list = hyAdService.getAdList(adApkFrom);
    if(list.size() > 0){
      return R.ok().put("data", list);
    }else{
      return R.ok();
    }
  }

  @PostMapping("/ad/get/{id}")
  @ApiOperation("通过广告id获取广告实时资源")
  public R getAdById(@PathVariable(value = "id") Integer id) {
    Assert.notNull(id, "id为空");
    HyAdEntity hyAdEntity = hyAdService.getAdById(id);
    if (ObjectUtil.isNull(hyAdEntity)) {
      return R.error("广告不存在或者已经下架");
    } else {
      Map map = new HashMap(2);
      map.put("img", hyAdEntity.getImg());
      map.put("url", hyAdEntity.getUrl());
      return R.ok().put("data",map);
    }
  }


  /**
   * 移动端获取充值列表，比例以及折扣类型
   *
   * @return
   */
  @GetMapping("charge/get")
  @ApiOperation("移动端获取充值模板列表，比例")
  public R getCharge() {
    try {
      Map<String, Object> params = new HashMap<>();
      params.put("orderByField", "money");
      params.put("status", FrogConstant.BasicStatus.TRUE.getValue());
      Page page = hyChargeService.selectPageByConditions(new QueryExt(params));
      HyProductionScaleEntity vipScale = hyProductionVacuateService.getOne(new QueryWrapper<HyProductionScaleEntity>().eq(
              "param_key",FrogConstant.SysConfigKey.ANDROID_CHARGE_SCALE));
      if (page.getRecords().size() > 0) {
        return R.ok().put("page", page).put("scale",vipScale);
      } else {
        return R.error("获取失败");
      }
    } catch (MyException e) {
      return R.error(e.getMessage());
    }
  }

  /* 点播视频 */
  /**
   * 移动端获取类型
   * @return
   */
  @GetMapping(value = "/videoType/get")
  public R getTypes() {
    try {
      List<HyVideoTypeEntity> result=hyVideoTypeService.getBaseMapper().selectList(
              new QueryWrapper<HyVideoTypeEntity>().eq("status", Constants.CommonStatus.NORMAL).orderByAsc("sort"));
      return R.ok().put("data",result);
    } catch (ServiceException e) {
      return R.error(e.getMessage());
    }
  }

  /**
   * 查询点播资源根据点播资源的类别 * @param typeId  类型的ID  * @param sourceNum  渠道号
   * @return
   */
  @PostMapping(value = "vod/get")
  public R getVodByType(@RequestBody VodQueryForm form) {
    try {
      ValidatorUtils.validateEntity(form);
      Map map= BeanUtil.beanToMap(form);
      IPage page = hyVideoVodService.findVideoVodListByTypeAndChannel(new QueryExt(map));
      return R.ok().put("data", page);
    } catch (ServiceException e) {
      return R.error(e.getMessage());
    }
  }


  /**
   * 根据视频资源的ID获取点播的视频资源
   * @param id
   * @return
   */
  @PostMapping(value = "vod/detail/{id}")
  public R getVodByType(@PathVariable Integer id) {
    try {
      VodVideoVo result = hyVideoVodService.getVodVideoByVid(id);
      return R.ok().put("detail",result);
    } catch (ServiceException e) {
      return R.error(e.getMessage());
    }
  }

  /* 小视频 */
  @PostMapping(value = "smallVideo/get")
  public R smallVideoList(@RequestBody Map map) {
    try {
      IPage page = hyVideoSmallService.getSmallVideoPage(new QueryExt(map));
      return R.ok().put("data",page);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /**
   * 小视频详情
   */
  @PostMapping(value = "smallVideo/detail/{id}")
  public R smallVideoDetail(@PathVariable Integer id) {
    try {
      HyVideoSmallEntity entity=hyVideoSmallService.getBaseMapper().selectById(id);
      return R.ok().put("data",entity);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  @PostMapping(value = "smallVideo/play/{id}")
  public R smallVideoPlay(@PathVariable Integer id) {
    try {
      // 播放量+1
      HyVideoSmallEntity ent=hyVideoSmallService.getById(id);
      ent.setAttention(ent.getAttention()+1);
      ent.setDayAttention(ent.getDayAttention()+1);
      hyVideoSmallService.updateById(ent);
      return R.ok("播放成功");
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  // 分享小视频
  @PostMapping(value = "smallVideo/share/{id}")
  public R smallVideoShare(@PathVariable Integer id) {
    try {
      return R.ok().put("data",hyVideoSmallService.getById(id));
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }

  /**
   * 查看小视频评论
   */
  @PostMapping(value = "smallVideo/comment/{id}")
  public R getSmallVideoCommentbyId(@PathVariable Integer id) {
    try {
      //QueryWrapper qw=new QueryWrapper<HyVideoSmallCommentEntiy>().eq("wid",id).orderByDesc("create_time");
      QueryExt queryExt=new QueryExt(new HashMap<>());
      IPage page=hyVideoSmallCommentService.getCommentsById(queryExt,id);
      return R.ok().put("data",page);
    } catch (Exception e) {
      return R.error(e.getMessage());
    }
  }



}
